package io.renren.modules.basedata.service.impl;

import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.MobileServiceUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.MobileServiceUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.MobileServiceUploadDao;
import io.renren.modules.basedata.entity.tbl_mobileService;
import io.renren.modules.basedata.service.MobileServiceUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


@Service
public class MobileServiceUploadServiceImpl implements MobileServiceUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private MobileServiceUploadDao mobileServiceUploadDao;

    @Override
    public String mobileServiceUpload() throws Exception {
        log.info("开始获取流动服务网点信息！");
        int operResult = 0;
        List<tbl_mobileService> list = null;
        tbl_mobileService mobileService = null;
        list = getMobileServiceList();//获取流动服务网点信息
        if(list!=null&&list.size()>0){
            //一条一条上传数据
            for(int i=0;i<list.size();i++){
                mobileService = list.get(i);
                MobileServiceUploadRequest request = new MobileServiceUploadRequest();
                request.setId(mobileService.getId());
                request.setStartTime(DateUtil.formatDate(mobileService.getStartTime()));
                request.setEndTime(DateUtil.formatDate(mobileService.getEndTime()));
                request.setOperation(mobileService.getFlag());

                String fileName = ETCCommHelper.getFileName(request,"65");
                //生成就json文件
//					CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);

                try{
                    MobileServiceUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, MobileServiceUploadResponse.class);
                    log.info("上传流动服务网点信息服务器返回："+response.toJson());

                    if(response!=null&&response.getInfo().equals("成功！")){//表示数据上传成功
                        //更新流动服务网点信息表中的上传标志
                        if(mobileServiceUploadDao.updateMobileServiceUploadflag(mobileService.getId())==1){
                            log.info("流动服务网点表上传标志更新成功！");
                        }else{
                            log.info("流动服务网点表上传标志更新失败！");
                        }
                    }else{
                        log.info("上传流动服务网点信息失败！失败原因："+response.getInfo());

                        mobileServiceUploadDao.updateMobileServiceUploadflagError(mobileService.getId());

                    }
                }catch(Exception e){
                    log.error("上传流动服务网点信息失败！错误信息：",e);

                    mobileServiceUploadDao.updateMobileServiceUploadflagError(mobileService.getId());
                    e.printStackTrace();
                    continue;
                }
            }
            return "上传流动服务网点信息结束！";
        }else{
            return "无流动服务网点信息，上传结束...";
        }
    }

    //获取流动服务网点信息
    @SuppressWarnings("unchecked")
    public List<tbl_mobileService> getMobileServiceList() throws IOException{
        List<tbl_mobileService> list;
        try{
            list = mobileServiceUploadDao.getMobileServiceList();
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取流动服务网点信息失败！错误信息",e);
            throw new IOException("获取流动服务网点信息失败:"+e.getMessage());
        }
        return list;
    }



}
